﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>ControlGetHwnd - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The ControlGetHwnd function returns the unique ID number of the specified control." />
<meta name="ahk:equiv-v1" content="commands/ControlGet.htm#Hwnd" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>ControlGetHwnd</h1>

<p>返回指定控件的唯一 ID.</p>

<pre class="Syntax">Hwnd := <span class="func">ControlGetHwnd</span>(Control <span class="optional">, WinTitle, WinText, ExcludeTitle, ExcludeText</span>)</pre>

<h2 id="Parameters">参数</h2>
<dl>
    <dt>Control</dt>
    <dd>
        <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
        控件的 ClassNN, 文本或 HWND, 或具有 <code>Hwnd</code> 属性的对象. 有关详情, 请参阅 <a href="Control.htm#Parameter">Control 参数</a>.</p>
    </dd>
    <dt>WinTitle</dt>
    <dd>
        <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
        <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
    </dd>
    <dt>WinText</dt>
    <dd>
        <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
        <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
    </dd>
    <dt>ExcludeTitle</dt>
    <dd>
        <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
        <p>标题中含有此参数值的窗口将被排除.</p>
    </dd>
    <dt>ExcludeText</dt>
    <dd>
        <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
        <p>文本中含有此参数值的窗口将被排除.</p>
    </dd>
</dl>

<h2 id="Return_Value">返回值</h2>
<p>类型: <a href="../Concepts.htm#numbers">整数</a></p>
<p>此函数返回指定控件的<a href="../misc/WinTitle.htm#ahk_id">窗口句柄(HWND)</a>.</p>

<h2 id="Error_Handling">错误处理</h2>
<p>如果找不到窗口或控件, 则抛出异常.</p>

<h2 id="Remarks">备注</h2>
<p>控件的 HWND 通常与 <a href="PostMessage.htm">PostMessage</a>, <a href="SendMessage.htm">SendMessage</a> 和 <a href="DllCall.htm">DllCall</a> 一起使用. 另外, 还可以通过 <a href="MouseGetPos.htm">MouseGetPos</a> 来获取控件的 HWND. 最后, 控件的 HWND 可以直接用作 <a href="../misc/WinTitle.htm#ahk_id">ahk_id WinTitle</a>. 这也适用于隐藏控件, 即使 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 为 Off.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>

<h2 id="Related">相关</h2>
<p><a href="WinGetID.htm">WinGetID</a>, <a href="../objects/GuiControl.htm#Hwnd">Hwnd 属性(GuiControl 对象)</a>, <a href="Control.htm">Control 函数</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<h3 id="ex1">示例 #1</h3>
<p><a href="#ExBasic">#1</a>: 检索记事本的 Edit 控件的唯一 ID:</p>
<pre>editHwnd := ControlGetHwnd("Edit1", "ahk_class Notepad")</pre>
</div>

</body>
</html>